<?php
namespace Admin\Controller;
use Api\Event\CacheEvent;
// +----------------------------------------------------------------------
// | 文件 InsiderRewardController.class.php
// +----------------------------------------------------------------------
// | 说明 内部打赏管理
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class InsiderRewardController extends AdminController 
{

    const DAILYKEY = "IMBA.BACKEND.INSIDER.DAILYMONEY.";

    public function index()
    {
        $date  = date('Ymd');
        $cache = CacheEvent::instance(3);
        $key   = static::DAILYKEY.$date;
        $users = $cache->zrevrange($key, 0, -1);
        if ($users){
            $this->assign('has_gave', 1);
        }

        $uids = C('BACKEND_INSIDER');
        $uids && $uids = explode(',', $uids);
        $this->assign('uids', $uids);
        $this->display();
    }

    /**
     * 发放每日的金钱
     * @return [type] [description]
     */
    public function give()
    {
        $uids = C('BACKEND_INSIDER');
        $uids && $uids = explode(',', $uids);
        $money = I('money');
        if (!$uids || !$money){
            $this->error('没有人员或者金额为0');
        }
        $date  = date('Ymd');
        $cache = CacheEvent::instance(3);
        $key   = static::DAILYKEY.$date;
        $users = $cache->zrevrange($key, 0, -1);
        if ($users){
            $this->error('今日已经发放过了');
        }
        $LocalClientEvent = event('LocalClient');
        foreach ($uids as $uid) {
            // 发放金额
            $param = [
                'uid'       => $uid,
                'money'     => $money,
                'admin_uid' => UID
            ];
            $res = $LocalClientEvent->doRequest('admin_gift_coin', $param);
            if ($res['code'] == 200){
                $cache->zadd($key, $money, $uid);
            }
        }
        $this->success('发放成功');
    }

    /**
     * 查看统计
     * @return [type] [description]
     */
    public function statis()
    {
        $uids = C('BACKEND_INSIDER');
        $uids && $uids = array_map('intval',explode(',', $uids));
        $date = I('date', date('Y-m-d'));
        

        #获取发放统计
        $gifts = D('PayLog')->where(['date' => $date, 'type' => 'admin_gift'])->group('uid')->field('uid, sum(money) as total')->select();
        $gifts = array_column($gifts, 'total', 'uid');
        #获取打赏统计
        $rewards = D('PayLog')->where(['date' => $date, 'type' => 'reward'])->group('uid')->field('uid, sum(money) as total')->select();
        $rewards = array_column($rewards, 'total', 'uid');
        $statis = [];
        foreach ($gifts as $uid => $money) {
            $statis[$uid] = [
                'gifts'     => $money,
                'rewards'   => $rewards[$uid] ?: 0,
            ];
        }

        #发布的评论个数
        $start_time = strtotime($date);
        $end_time   = strtotime($date . ' 23:59:59');


        $map['$and'] = [
            ['uid' => $uids ? ['$in' => $uids] : ''],
            ['create_time' => ['$gte' => $start_time]],
            ['create_time' => ['$lte' => $end_time]],
        ];
        $where['_array'] = $map;
        $comments = service('Comments')->where($where)->select();
        foreach ($comments as $com) {
            $uid = $com['uid'];
            $statis[$uid]['comments']++;
            $statis[$uid]['doreply']++;
        }
        $map = [];
        $map['$and'] = [
            ['uid' => $uids ? ['$in' => $uids] : ''],
            ['created_at' => ['$gte' => $start_time]],
            ['created_at' => ['$lte' => $end_time]],
        ];
        $where['_array'] = $map;
        $posts = service('Posts')->where($where)->select();
        foreach ($posts as $post) {
            $uid = $post['uid'];
            $statis[$uid]['posts']++;
            $statis[$uid]['doreply']++;
        }

        $map = [];
        $map['$and'] = [
            ['uid' => $uids ? ['$in' => $uids] : ''],
            ['type' => 3],
            ['created_at' => ['$gte' => $start_time]],
            ['created_at' => ['$lte' => $end_time]],
        ];
        $where['_array'] = $map;
        $replys = service('Reply')->where($where)->select();
        foreach ($replys as $reply) {
            $uid = $reply['uid'];
            $statis[$uid]['replys']++;
            $statis[$uid]['doreply']++;
        }

        $this->assign('date',  $date);
        $this->assign('statis', $statis);
        $this->display();
    }

}





